<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        h2 {
            width: 600px;
            text-align: center;
            margin: 50px auto;
        }
        
        table {
            width: 600px;
            margin: 0 auto;
            border-collapse: collapse;
        }
        
        tr {
            width: 100%;
        }
        
        th,
        td {
            border: 1px solid #ccc;
            width: 100px;
            height: 50px;
            text-align: center;
        }
        
        button {
            margin: 0 5px;
        }
        
        .box {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 400px;
            /* height: 800px; */
            border: 1px solid #ccc;
            background-color: #ccc;
            display: none;
        }
        
        .box ul {
            margin: 50px 0 20px;
        }
        
        .box li {
            height: 30px;
            line-height: 30px;
            list-style: none;
        }
        
        .box li label {
            display: inline-block;
            width: 100px;
            height: 30px;
            line-height: 30px;
            text-align: right;
            /* background-color: pink; */
        }
        
        .box li input[type="text"] {
            width: 250px;
            height: 30px;
            padding: 0;
            margin: 0;
        }
        
        .box li input[type="radio"] {
            margin-left: 20px;
        }
        
        .box #btn {
            width: 100px;
            height: 30px;
            margin: 0 auto;
            margin-bottom: 50px;
        }
        
        .box #btn button {
            display: inline-block;
            padding: 10px 20px;
        }
    </style>
</head>

<body>
    <h2>用户列表</h2>
    <table>
        <thead>
            <tr>
                <th>用户名</th>
                <th>年龄</th>
                <th>性别</th>
                <th>职位</th>
                <th>是否退休</th>
                <th><button class="add">添加</button></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="uName">勿忘我1</td>
                <td class="age">18</td>
                <td class="sex">女</td>
                <td class="office">经理</td>
                <td class="state">否</td>
                <td>
                    <button class="update">修改</button>
                    <button class="delete">删除</button>
                </td>
            </tr>
            <tr>
                <td class="uName">勿忘我1</td>
                <td class="age">18</td>
                <td class="sex">女</td>
                <td class="office">经理</td>
                <td class="state">否</td>
                <td>
                    <button class="update">修改</button>
                    <button class="delete">删除</button>
                </td>
            </tr>
            <tr>
                <td class="uName">勿忘我1</td>
                <td class="age">18</td>
                <td class="sex">女</td>
                <td class="office">经理</td>
                <td class="state">否</td>
                <td>
                    <button class="update">修改</button>
                    <button class="delete">删除</button>
                </td>
            </tr>
            <tr>
                <td class="uName">勿忘我1</td>
                <td class="age">18</td>
                <td class="sex">女</td>
                <td class="office">经理</td>
                <td class="state">否</td>
                <td>
                    <button class="update">修改</button>
                    <button class="delete">删除</button>
                </td>
            </tr>
            <tr>
                <td class="uName">勿忘我1</td>
                <td class="age">18</td>
                <td class="sex">女</td>
                <td class="office">经理</td>
                <td class="state">否</td>
                <td>
                    <button class="update">修改</button>
                    <button class="delete">删除</button>
                </td>
            </tr>
        </tbody>
    </table>

    <div class="box">
        <ul>
            <li><label>用户名：</label><input type="text" class="uName" /></li>
            <br />
            <li><label>年龄：</label><input type="text" class="age" /></li>
            <br />
            <li>
                <label>性别：</label>
                <input type="radio" name="sex" class="sex" checked="checked" value="男" />男 <input type="radio" name="sex" class="sex" value="女" />女
            </li>
            <br />
            <li><label>职位：</label><input type="text" class="office" /></li>
            <br />
            <li>
                <label>是否退休：</label>
                <input type="radio" name="state" class="state" checked="checked" value="否" />否 <input type="radio" name="state" class="state" value="是" />是
            </li>
            <br />
        </ul>
        <div id="btn">
            <button class="add">添加</button>
            <button class="update">修改</button>
        </div>
    </div>

    <script src="./jquery-1.12.4.js"></script>
    <script>
        $(function() {
            console.log($("td .delete"));
            // 删除
            $("table tbody").on("click", "td .delete", function() {
                $(this).parents("tr").remove();
            });

            // 修改
            $("table tbody").on("click", "td .update", function() {
                var that = $(this);
                $(".box").show();
                $("#btn .update").show();
                $("#btn .add").hide();
                $("#btn .update").click(function() {
                    if ($(".box  .uName").val() !== "") {
                        that.parents("tr").find(".uName").text($(".box .uName").val());
                    }
                    if ($(".box  .age").val() !== "") {
                        if (isNaN($(".box  .age").val())) {
                            alert("年龄错误，请输入数字！");
                            $("table tbody tr td .update").click();
                        } else {
                            that.parents("tr").find(".age").text($(".box .age").val());
                            $(".box").hide();
                        }
                    }
                    for (var i = 0; i < $(".box .sex").length; i++) {
                        if ($(".box  .sex").get(i).checked) {
                            that
                                .parents("tr")
                                .find(".sex")
                                .text($(".box .sex").get(i).value);
                        }
                    }

                    if ($(".box  .office").val() !== "") {
                        that.parents("tr").find(".office").text($(".box .office").val());
                    }
                    for (var i = 0; i < $(".box .state").length; i++) {
                        if ($(".box  .state").get(i).checked) {
                            that
                                .parents("tr")
                                .find(".state")
                                .text($(".box .state").get(i).value);
                        }
                    }
                });
            });

            // 添加
            $("table .add").on("click", function() {
                $(".box").show();
                $("#btn .update").hide();
                $("#btn .add").show();
                $("#btn .add").on("click", function() {
                    if ($(".box  .uName").val() !== "") {
                        if ($(".box  .age").val() !== "") {
                            if (isNaN($(".box  .age").val())) {
                                alert("年龄错误，请输入数字！");
                                $("table tbody tr td .update").click();
                            } else {
                                for (var i = 0; i < $(".box .sex").length; i++) {
                                    if ($(".box  .sex").get(i).checked) {
                                        if ($(".box  .office").val() !== "") {
                                            if ($(".box  .state").val() !== "") {
                                                for (var i = 0; i < $(".box .state").length; i++) {
                                                    if ($(".box  .state").get(i).checked) {
                                                        var str = `<tr>
                                                                    <td class="uName">${$(
                                                                      ".box .uName"
                                                                    ).val()}</td>
                                                                    <td class="age">${$(
                                                                      ".box .age"
                                                                    ).val()}</td>
                                                                    <td class="sex">${
                                                                      $(
                                                                        ".box .sex"
                                                                      ).get(i)
                                                                        .value
                                                                    }</td>
                                                                    <td class="office">${$(
                                                                      ".box .office"
                                                                    ).val()}</td>
                                                                    <td class="state">${
                                                                      $(
                                                                        ".box .state"
                                                                      ).get(i)
                                                                        .value
                                                                    }</td>
                                                                    <td>
                                                                        <button class="update">修改</button>
                                                                        <button class="delete">删除</button>
                                                                    </td>
                                                                </tr>`;
                                                        $("table tbody").append(str);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                $(".box").hide();
                            }
                        }
                    }
                });
            });
        });
    </script>
</body>

</html>